package com.victor.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.victor.dao.JingquDao;
import com.victor.entity.Jingdianintroduce;
import com.victor.entity.Jingdiantupian;
import com.victor.entity.Jingquclass;
import com.victor.entity.Jingqucomment;
import com.victor.entity.Jingqucommentphoto;
import com.victor.entity.Jingqujingdian;
import com.victor.entity.Jingqulist;
import com.victor.entity.Jingqutupian;
import com.victor.entity.JqCollection;
import com.victor.entity.JqReply;
import com.victor.entity.JqcommentPraise;
import com.victor.entity.JqcommentsReply;
import com.victor.service.JingquService;

@Service
public class JingquServiceImpl implements JingquService {

	@Autowired
	private JingquDao dao;

	// 获取所有景区类别
	@Override
	public List<Jingquclass> getClassList() {
		return dao.getClassList();
	}

	// 添加景区类型
	@Override
	public boolean addClass(Jingquclass jingquclass) {
		return dao.addClass(jingquclass) > 0 ? true : false;
	}

	// 修改类别信息
	@Override
	public boolean updateClass(Jingquclass jingquclass) {
		return dao.updateClass(jingquclass) > 0 ? true : false;
	}

	// 删除类别
	@Override
	public boolean deleteClass(int[] cids) {
		return dao.deleteClass(cids) > 0 ? true : false;
	}

	// 获取景区列表
	@Override
	public List<Jingqulist> getJQlist(String jqclassid, String starttime, String endtime, String searchname) {
		return dao.getJQlist(jqclassid, starttime, endtime, searchname);
	}

	// 新建景区--添加景区信息
	@Override
	public boolean buildJQ(Jingqulist jingqulist) {
		return dao.buildJQ(jingqulist) > 0 ? true : false;
	}

	// 添加景区图片信息
	@Override
	public boolean addJingqutupian(Jingqutupian jingqutupian) {
		return dao.addJingqutupian(jingqutupian) > 0 ? true : false;
	}

	// 批量删除
	@Override
	public boolean batchDeleteJQ(int[] jqids) {
		return dao.batchDeleteJQ(jqids) > 0 ? true : false;
	}

	// 景区详情
	@Override
	public Jingqulist getJQxqByid(String jqid) {
		return dao.getJQxqByid(jqid);
	}

	// 修改景区信息
	@Override
	public boolean updateJQ(Jingqulist jingqulist) {
		return dao.updateJQ(jingqulist) > 0 ? true : false;
	}

	// 添加景点
	@Override
	public boolean addJingdian(Jingqujingdian jingqujingdian) {
		return dao.addJingdian(jingqujingdian) > 0 ? true : false;
	}

	// 添加景点图片
	@Override
	public boolean addJingdiantupian(Jingdiantupian jingdiantupian) {
		return dao.addJingdiantupian(jingdiantupian) > 0 ? true : false;
	}

	// 删除景点
	@Override
	public boolean deleteJingdianByid(String jdid) {
		return dao.deleteJingdianByid(jdid) > 0 ? true : false;
	}

	// 添加景点简介
	@Override
	public boolean addJdintroduce(Jingdianintroduce jingdianintroduce) {
		return dao.addJdintroduce(jingdianintroduce) > 0 ? true : false;
	}

	// 删除评论
	@Override
	public boolean deleteCommentByid(String cid) {
		return dao.deleteCommentByid(cid) > 0 ? true : false;
	}

	// 修改景点
	@Override
	public boolean updateJD(Jingqujingdian jingqujingdian) {
		return dao.updateJD(jingqujingdian);
	}

	// 删除景点原图片
	@Override
	public boolean deleteJingdianTup(Integer jdid) {
		return dao.deleteJingdianTup(jdid) > 0 ? true : false;
	}

	// 删除景点原简介
	@Override
	public boolean deleteJingdianJJ(Integer jdid) {
		return dao.deleteJingdianJJ(jdid) > 0 ? true : false;
	}

	@Override
	public PageInfo<Jingqulist> getJingquListAll(int page, int row) {
		PageHelper.startPage(page, row);
		List<Jingqulist> list = dao.selAll();
		if (list != null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				List<Jingqutupian> tupian = dao.selJingqutupianById(list.get(i).getId());
				if (tupian == null || tupian.size()<=0) {
					list.get(i).setPhoto(null);
				}else {
					list.get(i).setPhoto(String.valueOf(tupian.get(0).getJqphoto()));
				}
				
			}
		}
		return new PageInfo<>(list, 5);
	}

	@Override
	public PageInfo<Jingqulist> selByCondition(HashMap<Object, Object> map,int page,int row) {
		PageHelper.startPage(page, row);
		List<Jingqulist> list = dao.selByCondition(map);
		if (list != null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				List<Jingqutupian> tupian = dao.selJingqutupianById(list.get(i).getId());
				if (tupian == null || tupian.size()<=0) {
					list.get(i).setPhoto(null);
				}else {
					list.get(i).setPhoto(String.valueOf(tupian.get(0).getJqphoto()));
				}
				
			}
		}
		return new PageInfo<>(list, 5);
	}

	@Override
	public int deleteList(int[] ids) {
		return dao.deleteByPrimaryKey(ids);
	}

	@Override
	public List<Jingqulist> getAllListNopageandrow() {
		return dao.selAll();
	}

	@Override
	public Jingqulist selByJqid(String jqid) {
		return dao.selectByPrimaryKey(jqid);
	}

	@Override
	public PageInfo<Jingqucomment> selJingquComments(String jqid,int page,int row,String flag) {
		PageHelper.startPage(page, row);
		List<Jingqucomment> list = dao.selcomments(jqid,flag);
		if (list != null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				List<JqcommentsReply> cJqcommentsReplies = dao.selCommentsReply(list.get(i).getId());
				String[] commentsreplysize = new String[cJqcommentsReplies.size()];
				if (cJqcommentsReplies != null && cJqcommentsReplies.size()>0) {
					for (int j = 0; j < cJqcommentsReplies.size(); j++) {
						commentsreplysize[j]=String.valueOf(cJqcommentsReplies.get(j).getId());
					}
				}
				List<JqReply> replies = dao.selReply(commentsreplysize);
				int replycount = cJqcommentsReplies.size()+replies.size();
				list.get(i).setReplycount(replycount);
				
			}
		}
		return new PageInfo<>(list, 5);
	}

	@Override
	public Jingqulist selByJqidAndUid(String jqid, String uid) {
		return dao.selectByPrimaryKeyAndUid(jqid,uid);
	}

	@Override
	public PageInfo<Jingqucomment> seljingquCommentsByUid(String jqid, int page, int row, String flag, String uid) {
		PageHelper.startPage(page, row);
		List<Jingqucomment> list = dao.selcommentsByUid(jqid,flag,uid);
		if (list != null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				List<JqcommentsReply> cJqcommentsReplies = dao.selCommentsReply(list.get(i).getId());
				String[] commentsreplysize = new String[cJqcommentsReplies.size()];
				if (cJqcommentsReplies != null && cJqcommentsReplies.size()>0) {
					for (int j = 0; j < cJqcommentsReplies.size(); j++) {
						commentsreplysize[j]=String.valueOf(cJqcommentsReplies.get(j).getId());
					}
				}
				List<JqReply> replies = dao.selReply(commentsreplysize);
				int replycount = cJqcommentsReplies.size()+replies.size();
				list.get(i).setReplycount(replycount);
				
			}
		}		
		return new PageInfo<>(list, 5);
	}

	@Override
	public boolean insertJqCollection(JqCollection collection) {
		return dao.insertJqCollection(collection);
	}

	@Override
	public JqcommentPraise selJqCommentsPraise(JqcommentPraise praise) {
		return dao.selJqCommentsPraise(praise);
	}

	@Override
	public boolean insertJqCommentsPraise(JqcommentPraise praise) {
		return dao.insertJqCommentsPraise(praise);
	}

	@Override
	public String selPraiseCount(String jcid) {
		return dao.selPraiseCount(jcid);
	}

	@Override
	public boolean delCommentsPraise(Integer id) {
		return dao.delCommentsPraise(id);
	}

	@Override
	public boolean insertJqComments(Jingqucomment cJingqucomment) {
		return dao.insertComments(cJingqucomment);
	}

	@Override
	public boolean insertJqCommentsPhoto(Jingqucommentphoto jingqucommentphoto) {
		return dao.insertJqCommentsPhoto(jingqucommentphoto);
	}

	@Override
	public boolean insertJqcommentsReply(JqcommentsReply reply) {
		return dao.insertJqCommentsReply(reply);
	}

	@Override
	public boolean insertReply(JqReply reply) {
		return dao.insertReply(reply);
	}

	@Override
	public JqCollection selCollection(JqCollection collection) {
		return dao.selCollection(collection);
	}

	@Override
	public boolean delJqCollection(Integer id) {
		return dao.delCollection(id);
	}

	@Override
	public List<Jingqulist> selByMuchCondition(String city,String search, String jqclassid, String tjflag, String jqlevel,
			String price) {
		return dao.selByMuchCondition(city,search,jqclassid,tjflag,jqlevel,price);
	}

	@Override
	public PageInfo<Jingqulist> selJqYouLike(String city, int page, int row) {
		PageHelper.startPage(page, row);
		List<Jingqulist> list = dao.selByMuchCondition(city, null, null, null, null, null);
		return new PageInfo<>(list, 5);
	}


}
